package com.tanhua.autoconfig.template;


import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.tanhua.autoconfig.properties.SmsProperties;

public class SmsTemplate {

    private SmsProperties properties;

    public SmsTemplate (SmsProperties properties) {
        this.properties = properties;
    }

    /**
     * 发送短信验证码
     *    参数：手机号，验证码
     */
    public void sendSms(String mobile,String code) throws Exception {

        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(properties.getAccessKey())
                // 您的AccessKey Secret
                .setAccessKeySecret(properties.getSecret())
                .setEndpoint("dysmsapi.aliyuncs.com");
        // 访问的域名
        com.aliyun.dysmsapi20170525.Client client = new com.aliyun.dysmsapi20170525.Client(config);


        //参数
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(mobile) //目标手机号
                .setSignName(properties.getSignName()) //签名名称
                .setTemplateCode(properties.getTemplateCode()) //短信模板code
                .setTemplateParam("{\"code\":\""+code+"\"}"); //模板中变量替换

        //发送短信
        SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);

        SendSmsResponseBody body = sendSmsResponse.getBody();

        // code = OK 代表成功
        System.out.println(body.getCode() + "  " + body.getMessage());
    }
}
